﻿CREATE TABLE [dbo].[Mobile_ListingSubmission] (
    [ID]             UNIQUEIDENTIFIER CONSTRAINT [DF_Mobile_ListingSubmission_ID] DEFAULT (newid()) NOT NULL,
    [Name]           NVARCHAR (50)    NULL,
    [Phone]          NVARCHAR (20)    NULL,
    [Email]          NVARCHAR (50)    NULL,
    [Company]        NVARCHAR (50)    NULL,
    [Price]          VARCHAR (50)     NULL,
    [Address]        NVARCHAR (200)   NULL,
    [Remark]         NVARCHAR (200)   NULL,
    [SubmissionType] NCHAR (10)       NOT NULL,
    [CreatedDate]    DATETIME         CONSTRAINT [DF_Mobile_ListingSubmission_CreatedDate] DEFAULT (getdate()) NULL,
    [isDelete]       SMALLINT         CONSTRAINT [DF_Mobile_ListingSubmission_isDelete] DEFAULT ((0)) NULL,
    [UpdateDate]     DATETIME         NULL,
    [UserID]         BIGINT           NULL,
    CONSTRAINT [PK_Mobile_ListingSubmission] PRIMARY KEY CLUSTERED ([ID] ASC)
);




GO
CREATE TRIGGER [dbo].[Mobile_ListingSubmission_dss_update_trigger] ON [dbo].[Mobile_ListingSubmission] FOR UPDATE AS
SET NOCOUNT ON
DECLARE @marker_create_scope_local_id INT
DECLARE @marker_scope_create_peer_timestamp BIGINT
DECLARE @marker_scope_create_peer_key INT
DECLARE @marker_local_create_peer_timestamp BIGINT
DECLARE @marker_local_create_peer_key INT
DECLARE @marker_state INT
SELECT TOP 1 @marker_create_scope_local_id = [provision_scope_local_id], @marker_local_create_peer_timestamp = [provision_timestamp], @marker_local_create_peer_key = [provision_local_peer_key], @marker_scope_create_peer_timestamp = [provision_scope_peer_timestamp], @marker_scope_create_peer_key = [provision_scope_peer_key], @marker_state = [state]
FROM [DataSync].[provision_marker_dss]
WHERE [object_id] = 139147541 
 AND [owner_scope_local_id] = 0

MERGE [DataSync].[Mobile_ListingSubmission_dss_tracking] AS [target] 
USING (SELECT [i].[ID] FROM INSERTED AS [i]) AS source([ID]) 
ON ([target].[ID] = [source].[ID])
WHEN MATCHED THEN
UPDATE SET [sync_row_is_tombstone] = 0, 
[local_update_peer_key] = 0, 
[update_scope_local_id] = NULL, [last_change_datetime] = GETDATE()
WHEN NOT MATCHED THEN
INSERT (
[ID] ,
[create_scope_local_id], [scope_create_peer_key], [scope_create_peer_timestamp], [local_create_peer_key], [local_create_peer_timestamp], [update_scope_local_id], [local_update_peer_key], [sync_row_is_tombstone], [last_change_datetime]) 
VALUES (
[source].[ID],@marker_create_scope_local_id, @marker_scope_create_peer_key, @marker_scope_create_peer_timestamp, 0, @marker_local_create_peer_timestamp , NULL, 0, 0, GETDATE() );
GO
CREATE TRIGGER [dbo].[Mobile_ListingSubmission_dss_insert_trigger] ON [dbo].[Mobile_ListingSubmission] FOR INSERT AS
SET NOCOUNT ON
DECLARE @marker_create_scope_local_id INT
DECLARE @marker_scope_create_peer_timestamp BIGINT
DECLARE @marker_scope_create_peer_key INT
DECLARE @marker_local_create_peer_timestamp BIGINT
DECLARE @marker_local_create_peer_key INT
DECLARE @marker_state INT
SELECT TOP 1 @marker_create_scope_local_id = [provision_scope_local_id], @marker_local_create_peer_timestamp = [provision_timestamp], @marker_local_create_peer_key = [provision_local_peer_key], @marker_scope_create_peer_timestamp = [provision_scope_peer_timestamp], @marker_scope_create_peer_key = [provision_scope_peer_key], @marker_state = [state]
FROM [DataSync].[provision_marker_dss]
WHERE [object_id] = 139147541 
 AND [owner_scope_local_id] = 0

MERGE [DataSync].[Mobile_ListingSubmission_dss_tracking] AS [target] 
USING (SELECT [i].[ID] FROM INSERTED AS [i]) AS source([ID]) 
ON ([target].[ID] = [source].[ID])
WHEN MATCHED THEN
UPDATE SET [sync_row_is_tombstone] = 0, 
[local_update_peer_key] = 0, 
[update_scope_local_id] = NULL, [last_change_datetime] = GETDATE()
WHEN NOT MATCHED THEN
INSERT (
[ID] ,
[create_scope_local_id], [scope_create_peer_key], [scope_create_peer_timestamp], [local_create_peer_key], [local_create_peer_timestamp], [update_scope_local_id], [local_update_peer_key], [sync_row_is_tombstone], [last_change_datetime]) 
VALUES (
[source].[ID],NULL, NULL, NULL, 0, CAST(@@DBTS AS BIGINT) + 1, NULL, 0, 0, GETDATE() );
GO
CREATE TRIGGER [dbo].[Mobile_ListingSubmission_dss_delete_trigger] ON [dbo].[Mobile_ListingSubmission] FOR DELETE AS
SET NOCOUNT ON
DECLARE @marker_create_scope_local_id INT
DECLARE @marker_scope_create_peer_timestamp BIGINT
DECLARE @marker_scope_create_peer_key INT
DECLARE @marker_local_create_peer_timestamp BIGINT
DECLARE @marker_local_create_peer_key INT
DECLARE @marker_state INT
SELECT TOP 1 @marker_create_scope_local_id = [provision_scope_local_id], @marker_local_create_peer_timestamp = [provision_timestamp], @marker_local_create_peer_key = [provision_local_peer_key], @marker_scope_create_peer_timestamp = [provision_scope_peer_timestamp], @marker_scope_create_peer_key = [provision_scope_peer_key], @marker_state = [state]
FROM [DataSync].[provision_marker_dss]
WHERE [object_id] = 139147541 
 AND [owner_scope_local_id] = 0

MERGE [DataSync].[Mobile_ListingSubmission_dss_tracking] AS [target] 
USING (SELECT [i].[ID] FROM DELETED AS [i]) AS source([ID]) 
ON ([target].[ID] = [source].[ID])
WHEN MATCHED THEN
UPDATE SET [sync_row_is_tombstone] = 1, 
[local_update_peer_key] = 0, 
[update_scope_local_id] = NULL, [last_change_datetime] = GETDATE()
WHEN NOT MATCHED THEN
INSERT (
[ID] ,
[create_scope_local_id], [scope_create_peer_key], [scope_create_peer_timestamp], [local_create_peer_key], [local_create_peer_timestamp], [update_scope_local_id], [local_update_peer_key], [sync_row_is_tombstone], [last_change_datetime]) 
VALUES (
[source].[ID],@marker_create_scope_local_id, @marker_scope_create_peer_key, @marker_scope_create_peer_timestamp, 0, @marker_local_create_peer_timestamp , NULL, 0, 1, GETDATE() );